package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.Auser;
import priv.mill.server.AuserService;

@Controller
@RequestMapping("Auser")
public class AuserController {

	@Autowired
	private AuserService AuserService;

	private static final Logger logger = Logger.getLogger(AuserController.class);

	@RequestMapping("getAuserById")
	@ResponseBody
	public RetenObj getAuserById(@RequestParam("AuserId") Integer AuserId) {
		try {
			return warpData(AuserService.getAuserById(AuserId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getAuserByEntry")
	@ResponseBody
	public RetenObj getAuserByEntry(Auser Auser) {
		try {
			return warpData(AuserService.getAuserByEntry(Auser), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getAusersByPage")
	@ResponseBody
	public RetenObj getAusersByPage(Auser Auser) {
		try {
			return warpData(AuserService.getAusersByPage(Auser), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateAuser")
	@ResponseBody
	public RetenObj updateAuser(Auser Auser) {
		try {
			int result = AuserService.updateAuser(Auser);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertAuser")
	@ResponseBody
	public RetenObj insertAuser(Auser Auser) {
		try {
			int result = AuserService.insertAuser(Auser);
			if (result > 0)
				return warpData(Auser.getUid(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteAuser")
	
	@ResponseBody
	public RetenObj deleteAuser(Integer AuserId) {
		try {
			int result = AuserService.deleteAuser(AuserId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
